﻿using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Input;
using DCT.ViewModel;

namespace DCT.View
{
    public class DeviceVisualHost : FrameworkElement
    {
        private VisualCollection _children;

        public VisualCollection Children
        {
            get { return _children; }
            set { _children = value; }
        }

        public DeviceVisualHost() : base()
        {
            _children = new VisualCollection(this);
            Loaded += new RoutedEventHandler(DeviceVisualHost_Loaded);
        }

        void DeviceVisualHost_Loaded(object sender, RoutedEventArgs e)
        {
            DeviceBaseView deviceView = this.TemplatedParent as DeviceBaseView;

            if (deviceView != null)
            {
                deviceView.DeviceVisual = this;
                deviceView.UpdateVisual();
            }
        }

        protected override int VisualChildrenCount
        {
            get { return _children.Count; }
        }

        protected override Visual GetVisualChild(int index)
        {
            if (index < 0 || index >= _children.Count)
            {
                throw new ArgumentOutOfRangeException();
            }

            return _children[index];
        }

        protected override void OnPreviewMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e)
        {
            base.OnPreviewMouseLeftButtonDown(e);

            DeviceBaseView deviceView = Util.GetParentVisual<DeviceBaseView>(this);

            if (deviceView != null &&
                deviceView.DeviceViewModel  != null &&
                deviceView.DeviceViewModel.DeviceMapViewModel != null &&
                !(deviceView.DeviceViewModel.DeviceMapViewModel.EditMode == EditModeState.DrawSwitch || deviceView.DeviceViewModel.DeviceMapViewModel.EditMode == EditModeState.DrawTrackSegment))
            {
                if (deviceView.DeviceViewModel is TrackSegmentViewModel)
                {
                    DeviceCanvas deviceCanvas = Util.GetParentVisual<DeviceCanvas>(deviceView);
                    TrackSegmentViewModel trackSegmentViewModel = deviceView.DeviceViewModel as TrackSegmentViewModel;

                    if (deviceCanvas != null)
                    {
                        if ((Keyboard.Modifiers & (ModifierKeys.Shift | ModifierKeys.Control)) != ModifierKeys.None)
                        {
                            trackSegmentViewModel.IsSelected = !trackSegmentViewModel.IsSelected;
                            trackSegmentViewModel.AnotherTSViewModel.IsSelected = trackSegmentViewModel.IsSelected;
                        }
                        else
                        {
                            deviceCanvas.DeviceMapViewModel.DeselectAll_Execute();
                            trackSegmentViewModel.IsSelected = true;
                            trackSegmentViewModel.AnotherTSViewModel.IsSelected = true;
                        }
                    }
                }

                if (deviceView.DeviceViewModel is SwitchViewModel)
                {
                    DeviceCanvas deviceCanvas = Util.GetParentVisual<DeviceCanvas>(deviceView);
                    SwitchViewModel switchViewModel = deviceView.DeviceViewModel as SwitchViewModel;

                    if (deviceCanvas != null)
                    {
                        if ((Keyboard.Modifiers & (ModifierKeys.Shift | ModifierKeys.Control)) != ModifierKeys.None)
                        {
                            switchViewModel.MergeSwViewModel.IsSelected = !switchViewModel.MergeSwViewModel.IsSelected;
                            switchViewModel.NormalSwViewModel.IsSelected = switchViewModel.MergeSwViewModel.IsSelected;
                            switchViewModel.ReverseSwViewModel.IsSelected = switchViewModel.MergeSwViewModel.IsSelected;
                        }
                        else
                        {
                            deviceCanvas.DeviceMapViewModel.DeselectAll_Execute();
                            switchViewModel.MergeSwViewModel.IsSelected = true;
                            switchViewModel.NormalSwViewModel.IsSelected = true;
                            switchViewModel.ReverseSwViewModel.IsSelected = true;
                        }
                    }
                }
            }
        }
    }
}
